2. 캔사스 주를 포함한 5개 주요 밀 생산 주의 밀 생산량과 미국전체의 밀 생산량과의 상관관계 – “캔사스 주에 흉년이 들면 밀 가격이 오를까?

미국 내에서 밀을 가장 많이 생산하는 5개주는 어느 곳일까? 다음의 링크를 따라가면 답을 알 수 있다. https://www.statista.com/statistics/190376/top-us-states-in-wheat-production/

캔사스, 노스 다코타, 몬타나, 워싱턴, 오클라호마 주이다.

library(ggplot2)
options(scipen = 100)
library(gridExtra)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data03 = read.csv("data/historical_wheat_production_by_state.csv", sep = ",", stringsAsFactor = FALSE)
head(data03, 10)
##    year Kansas Montana N.Dakota Washington Oklahoma five.state.total
## 1  1991  363.0   163.5    303.7       98.6    135.0           1063.8
## 2  1992  363.8   149.2    472.9      119.6    168.2           1273.7
## 3  1993  388.5   206.3    336.6      177.6    156.6           1265.6
## 4  1994  433.2   170.6    356.4      134.0    143.1           1237.3
## 5  1995  286.0   195.8    300.3      153.8    109.2           1045.1
## 6  1996  255.2   175.0    395.1      182.7     93.1           1101.1
## 7  1997  501.4   181.5    269.3      165.1    169.6           1286.9
## 8  1998  494.9   168.8    307.7      157.4    198.9           1327.7
## 9  1999  432.4   154.3    242.1      124.1    150.5           1103.4
## 10 2000  347.8   135.2    317.0      164.9    142.8           1107.7
##    total.wheat.production million.bushel
## 1                  1980.1             NA
## 2                  2466.7             NA
## 3                  2396.4             NA
## 4                  2320.9             NA
## 5                  2182.7             NA
## 6                  2277.3             NA
## 7                  2481.4             NA
## 8                  2547.3             NA
## 9                  2295.5             NA
## 10                 2228.1             NA

head는 상위 10개의 데이터를 항목별로 보여준다. year는 년도 Kansas, Montana, N. Dakota, Washington, Oklahoma 이렇게 다섯 개 주가 표시 되어 있고 단위는 백만 bushel 이라서 1991년 Kansas 주 363.0 은 실제로 363,000,000 bushel 이다. five.state.total 은 다섯 개 주의 밀 생산량을 합산한 것이다. total.wheat.production 은 미국 전체의 밀 생산량이다.

str(data03)
## 'data.frame':    28 obs. of  9 variables:
##  $ year                  : int  1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 ...
##  $ Kansas                : num  363 364 388 433 286 ...
##  $ Montana               : num  164 149 206 171 196 ...
##  $ N.Dakota              : num  304 473 337 356 300 ...
##  $ Washington            : num  98.6 119.6 177.6 134 153.8 ...
##  $ Oklahoma              : num  135 168 157 143 109 ...
##  $ five.state.total      : num  1064 1274 1266 1237 1045 ...
##  $ total.wheat.production: num  1980 2467 2396 2321 2183 ...
##  $ million.bushel        : logi  NA NA NA NA NA NA ...

str()은 각 변수의 속성에 대해서 알려준다. data01은 데이터 프레임이고 , 31개의 관측치, 10개 변수로 구성되어 있다는 걸 알 수 있다. year, Kansas, Montana, N.Dakota, Washington, Oklahoma 이렇게 5개 주의 연도별 밀 생산량을 표시하고 있다. five.state.total 은 5개 밀 생산량의 총합이고 total.wheat.production 은 미국 전체의 밀 생산량이다. 단위는 백만 bushel 이다.

summary(data03)
##       year          Kansas         Montana         N.Dakota    
##  Min.   :1991   Min.   :246.4   Min.   : 96.6   Min.   :199.8  
##  1st Qu.:1998   1st Qu.:289.9   1st Qu.:152.2   1st Qu.:287.6  
##  Median :2004   Median :353.9   Median :174.1   Median :307.6  
##  Mean   :2004   Mean   :354.9   Mean   :170.6   Mean   :314.1  
##  3rd Qu.:2011   3rd Qu.:383.8   3rd Qu.:195.6   3rd Qu.:349.4  
##  Max.   :2018   Max.   :501.4   Max.   :214.1   Max.   :472.9  
##    Washington       Oklahoma      five.state.total total.wheat.production
##  Min.   : 98.6   Min.   : 47.60   Min.   : 830.8   Min.   :1606          
##  1st Qu.:128.4   1st Qu.: 98.75   1st Qu.:1027.5   1st Qu.:2018          
##  Median :141.4   Median :125.05   Median :1102.6   Median :2173          
##  Mean   :141.2   Mean   :123.69   Mean   :1104.5   Mean   :2160          
##  3rd Qu.:154.7   3rd Qu.:151.57   3rd Qu.:1221.8   3rd Qu.:2312          
##  Max.   :182.7   Max.   :198.90   Max.   :1327.7   Max.   :2547          
##  million.bushel
##  Mode:logical  
##  NA's:28       
##                
##                
##                
## 

5개 각 주의 밀의 최소 생산량, 제1사분위수, 제2사분위수(중앙값), 평균, 제3사분위수는 , 최고가격을 표시하고 있다. 그리고 5개 주의 밀생산량 총합과 미국 전체의 밀생산량에 대해서 마찬가지이다.

stem(data03$five.state.total)
## 
##   The decimal point is 2 digit(s) to the right of the |
## 
##    8 | 3
##    8 | 9
##    9 | 04
##    9 | 677
##   10 | 
##   10 | 55669
##   11 | 0000134
##   11 | 
##   12 | 024
##   12 | 6779
##   13 | 03
hist(data03$five.state.total)

boxplot(data03$five.state.total)

stem(data03$total.wheat.production)
## 
##   The decimal point is 2 digit(s) to the right of the |
## 
##   16 | 1
##   17 | 4
##   18 | 18
##   19 | 589
##   20 | 356
##   21 | 03668
##   22 | 1358
##   23 | 0124
##   24 | 078
##   25 | 15
hist(data03$total.wheat.production)

boxplot(data03$total.wheat.production)

five.state.total 과 total.wheat.production 의 stem() , hist(), boxplot() 을 살펴 봤지만 정상으로 보이고 극단치는 없었다.

Null Hypothesis( 귀무가설: Ho 라고 쓴다. 검정 당사자가 타파하거나 기각시키려고 하는 가설입니다.)

Alternative Hypothesis (대립가설: Ha 라고 쓴다. 귀무가설에 반대되는 가설로 연구와 실험을 통해서 입증, 규명하고자 하는 가설이다. 즉, 대립 가설은 검정 당사자가 참이라고 믿거나 참으로 증명되기를 바라는 것입니다.)

귀무가설과 대립가설은 수학적으로 완전히 반대 상태에 있다. 즉, 두 가지의 가능성 만이 있고 하나가 참이면 다른 하나는 자동적으로 거짓이 된다.

Level of Confidence(신뢰도): 신뢰도(C)란 일관되고 재현이 가능한(replicable) 측정이 존재하는지 평가하는 기준이다. C-95%, C-99% 라는 식으로 표현한다.

Level of Significance(유의수준): 유의수준(알파a)이란 오류를 허용할 범위를 말하며 α = 1 – C 가 성립한다. 그래서 LOC(신뢰도)가 95% 일 때 α = 1 – C → α = 1 – 0.95 따라서 α = 0.05 가 된다.

Ho(귀무가설): 미국 전체의 밀 생산량과 5개 주요 밀 생산 주의 밀 생산량은 비례하지 않는다.

Ha(대립가설): 미국 전체의 밀 생산량과 5개 주요 밀 생산 주의 밀 생산량은 비례한다.

선형모형을 사용하여 회귀분석을 해보면 다음과 같다.

fit = lm(total.wheat.production ~ five.state.total, data = data03)

summary(fit)
## 
## Call:
## lm(formula = total.wheat.production ~ five.state.total, data = data03)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -212.39  -87.27   -0.38   67.80  354.38 
## 
## Coefficients:
##                  Estimate Std. Error t value      Pr(>|t|)    
## (Intercept)      534.3855   196.7367   2.716        0.0116 *  
## five.state.total   1.4717     0.1768   8.323 0.00000000835 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 125 on 26 degrees of freedom
## Multiple R-squared:  0.7271, Adjusted R-squared:  0.7166 
## F-statistic: 69.27 on 1 and 26 DF,  p-value: 0.000000008354

회귀분석 결과 Coefficients: 부분을 살펴보자. y절편(Intercept)은 534.3855 , 기울기는 1.4717 이다. p값(p-value)은 0.00000000835 로 매우 낮다. 즉 total.wheat.production 와 five.state.total 는 다음과 같은 관계가 성립한다.

total.wheat.production = 534.3855 + 1.4717 × five.state.total

기울기의 값이 플러스이므로 그래프로 그리면 우상향하는 직선일 것이다. 그리고 밀의 생산량과 가격 사이에는 비례 관계가 성립한다.

cor.test(data03$total.wheat.production, data03$five.state.total)
## 
##  Pearson's product-moment correlation
## 
## data:  data03$total.wheat.production and data03$five.state.total
## t = 8.323, df = 26, p-value = 0.000000008354
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.7033871 0.9299417
## sample estimates:
##       cor 
## 0.8527005

상관분석 결과 상관계수(r) 값은 0.8527005 이며 이 값의 제곱은 0.7271 로 회귀분석결과에서 나온 Multiple R-squared 값과 정확히 일치한다. P-Value 는 0.000000008354 로 매우 작다. 0.05보다 작으므로 귀무가설을 기각하고 대립가설을 채택한다. 따라서 밀의 생산량과 가격 사이에는 비례 관계가 성립한다

ggplot(data03, aes(x=five.state.total, y=total.wheat.production)) + geom_point() + stat_smooth(method = "lm")

t = ggplot(data03, aes(x=five.state.total, y=total.wheat.production)) + geom_point() + stat_smooth(method = "lm", level = 0.95)
ggplotly(t)

이제 위에서 분석한 결과를 그림으로 그려보면 위와 같이 나오는데 예상한 대로 우상향하는 직선으로 비례 관계를 보여주고 있다. 아래 그림은 인터액티브 그래프로 그렸다. 짙은 회색으로 된 구간이 그려져 있는데, 모수(평균)가 구간에 있을 확률이 95%이다. (95% 신뢰구간)

따라서, 캔사스 주에 밀농사에 흉년이 들면 밀가격은 오른다.